package cn.com.callback

import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.com.elite.EliteBean
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

class autoGetPost implements CallBackInvoke {

    private static final Logger log = LoggerFactory.getLogger(autoGetPost.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String paramString) {

        def httpURL = KHDBConfig.getAPIURL(eliteBean, "getPostByMarketId");
        def result = Util.doHttpPost(httpURL, "", "UTF-8", 5000, 0);
        JSONObject JsonDepartment = new JSONObject(result);
        log.info("[获取岗位接口调用结果]:" + JsonDepartment);
        int re_status = JsonDepartment.getInt("status")
        if (re_status == 200) {
            JSONArray jsonArray = JsonDepartment.getJSONArray("data");
            int dataArrayLen = jsonArray.length();
            JSONObject dataObject = null;
            for (int i = 0; i < dataArrayLen; i++) {
                dataObject = jsonArray.getJSONObject(i);
                def ram_id = KHUtil.getRandomString(6).toUpperCase();//随机id
                def id = KHUtil.GetJV(dataObject, "id");//岗位id
                def postName = KHUtil.GetJV(dataObject, "postName");//岗位名称
                def status = KHUtil.GetJV(dataObject, "status");//使用状态
				
				if(status=="0"){
                    status="1";
                }else if(status=="1"){
                    status="0";
                }

                def selectSql="select count(*) COUNT from role where dw_post_id='" + id + "'";
                def sqlResult= eliteBean.chaxun_sql(selectSql, 'dataSource');
                def row1 =sqlResult.get(0).get("COUNT");

                if(row1<=0){
                    def insertSql = "INSERT INTO role(ROLE_ID, ROLENAME, STATUS, CREATEDBY, CREATEDDATE, MODIFIEDBY, MODIFIEDDATE, dw_post_id) VALUES ('" + ram_id + "','" + postName + "', 1, 'SELITE', now(), 'SELITE', now(), '" + id + "')";
                    def rwo2= eliteBean.update(insertSql, 'dataSource');
                    log.debug("执行role数据新增SQL结果:" + rwo2);
                }else{
                    def updateSql = "update role set ROLENAME='" + postName + "',MODIFIEDDATE=now(),status='"+status+"' where dw_post_id='"+id+"'";
                    def rwo3= eliteBean.update(updateSql, 'dataSource');
                    log.debug("执行role数据更新SQL结果:" + rwo3);
                }
                /*
                if(row1<=0){
                    def insertSql = "insert into dw_post(id,postname,status,createddate) values('" + id + "','" + postName + "','" + status + "',now())";
                    def rwo2= eliteBean.update(insertSql, 'dataSource');
                    log.debug("执行dw_post数据新增SQL结果:" + rwo2);
                }else{
                    def updateSql = "update dw_post set postname='" + postName + "',modifydate=now(),status='"+status+"' where id='"+id+"'";
                    def rwo3= eliteBean.update(updateSql, 'dataSource');
                    log.debug("执行dw_post数据更新SQL结果:" + rwo3);
                }
                */
                /*
                def insertSql2 = "insert into role (ROLE_ID,ROLENAME,STATUS,CREATEDBY,CREATEDDATE,MODIFIEDBY,MODIFIEDDATE)\n" +
                        "select role_id, postname,case when status = 0 then 1 else 0 end,'SELITE',createddate,'SELITE',modifydate from dw_post where role_id not in (select role_id from role)";
                def rwo4= eliteBean.update(insertSql2, 'dataSource');
                log.debug("执行role数据新增SQL结果:" + rwo4);

                def updateSql2 = "update role R1,dw_post R2 set r1.ROLENAME =r2.postname WHERE r1.role_id = r2.role_id ";
                def rwo5= eliteBean.update(updateSql2, 'dataSource');
                log.debug("执行role数据更新SQL结果:" + rwo5);
                */
            }
            return null
        }
    }
}